package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.ShopDetailEntity;
import com.xilihui.xlh.business.entities.WithdrawRecordEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.business.widget.WithdrawPopWindow;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/9.
 */

public class WithdrawRecordActivity extends ToolBaseCompatActivity implements OnLoadMoreListener, OnRefreshListener, View.OnClickListener {
    
    @BindView(R.id.tv_toolbar_right)
    TextView tv_all;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    
    ExceptionManager exceptionManager;
    
    ArrayList<WithdrawRecordEntity.DataBean> data = new ArrayList<>();
    BaseAdapter<WithdrawRecordEntity.DataBean> baseAdapter;
    
    int page = 1;
    String status = "10086";
    
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void Sift(){

        WithdrawPopWindow popWindow = new WithdrawPopWindow(this);
        popWindow.showAsDropDown(tv_all,-100,5);
        
    }
    
    
    @Override
    public int getLayout() {
        return R.layout.activity_withdraw_record;
    }
    
    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("提现记录");
        setToolRightText("全部");
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_withdraw_recored;
            }
        };
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        baseAdapter = new BaseAdapter<WithdrawRecordEntity.DataBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_withdraw_record;
            }

            @Override
            protected void getView(BaseViewHolder holder, final WithdrawRecordEntity.DataBean entity, int position) {
                
                holder.setText(R.id.tv_title,entity.getRemark());
                holder.setText(R.id.tv_time,entity.getCreate_time());
                holder.setText(R.id.tv_num,entity.getMoney());
                holder.setText(R.id.tv_status,entity.getType());
                
                int status = Integer.valueOf(entity.getStatus());
                switch (status){
                    case -1:
                        holder.setTextColor(WithdrawRecordActivity.this,R.id.tv_status,R.color.tv_fb4e44);
                        break;
                    case 0:
                        holder.setTextColor(WithdrawRecordActivity.this,R.id.tv_status,R.color.tv_333333);
                        break;
                    case 1:
                        holder.setTextColor(WithdrawRecordActivity.this,R.id.tv_status,R.color.tv_59c04e);                        
                        break;
                    case 2:
                        holder.setTextColor(WithdrawRecordActivity.this,R.id.tv_status,R.color.tv_59c04e);
                        break;
                }
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(WithdrawRecordActivity.this,ShowDetailActivity.class);
                        intent.putExtra("id",entity.getId()+"");
                        startActivity(intent);
                    }
                });
                
            }
        };
        recyclerView.setAdapter(baseAdapter);
        
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    public void getData(boolean isLoading){

        MineRequest.withdrawRecord(WithdrawRecordActivity.this,page,status)
                .compose(DoTransform.<WithdrawRecordEntity>applyScheduler(this,isLoading))
                .compose(this.<WithdrawRecordEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<WithdrawRecordEntity>(this) {
                    @Override
                    protected void onSuccess(WithdrawRecordEntity entity) {
                        if(page==1){
                            data.clear();
                            if(entity.getData().size()<1){
                                exceptionManager.showEmpty();
                            }else {
                                exceptionManager.hide();
                            }
                        }

                        data.addAll(entity.getData());
                        baseAdapter.setList(data);
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }
    
    
    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        if(event.is(WithdrawPopWindow.WITHDRAW)){
            status = (String) event.getParams(0);
            page = 1;
            getData(true);
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        YEventBuses.unregister(this);
    }


    @Override
    public void onClick(View view) {
        
    }
}
